package kr.co.pension.usr.pe.re.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.pension.admin.co.ma.service.COMAService;
import kr.co.pension.admin.co.ma.vo.COMAVo;
import kr.co.pension.usr.pe.re.service.ChkService;
import kr.co.pension.usr.pe.re.vo.PEREVo;
import kr.co.pension.utils.AESSecurity;
import kr.co.pension.utils.BaseController;
import kr.co.pension.utils.PaginationInfo;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 펜션 예약관리
 * 
 * @author devhw
 * 
 */
@Controller
@RequestMapping("/chk/data/info/*")
public class ChkController extends BaseController {
	@Autowired
	private ChkService service;
	@Autowired
	private COMAService comaService;

	Logger log = Logger.getLogger(getClass());

	@RequestMapping
	public ModelAndView main(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView();

		vo = dataDesc(vo, req,service);
		
		mav.addObject("roll", vo);
		
		return mav;
	}

	@RequestMapping
	public ModelAndView usrList(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();


		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn")) && "Y".equals(vo.get("roll1"))){
			
			PaginationInfo pg = new PaginationInfo();
			int currnetPageNo = 1;
			if(vo.get("currentPageNo")!=null && !"".equals( vo.get("currentPageNo"))){
				currnetPageNo = Integer.parseInt( vo.get("currentPageNo").toString());
			}
			pg.setCurrentPageNo(currnetPageNo);
			pg.setParamMap(vo);

			int iCnt = service.usrListCnt(vo);

			vo.put("page", pg);
			jsonMap.put("list", service.usrList(vo));	
			jsonMap.put("roll2", vo.get("roll2"));
			pg.setTotalRecordCount(iCnt);
			mav.addObject("map", jsonMap);
		}
		return mav;
	}
	
	
	@RequestMapping
	public ModelAndView updateCnt(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();
		

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn")) && "Y".equals(vo.get("roll2"))){
			service.updateCnt(vo);
			jsonMap.put("returnCd", 0);
			mav.addObject("map", jsonMap);
		}
				
		return mav;
	}
	
	@RequestMapping
	public ModelAndView usrListM(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();
		

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn")) && "Y".equals(vo.get("roll1"))){

			jsonMap.put("list", service.usrListM(vo));
			mav.addObject("map", jsonMap);
		}
		return mav;
	}

	@RequestMapping
	public ModelAndView intUsr(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView();
		COMAVo codeVo = new COMAVo();
		// 가입사이트
		codeVo.setCode_p_no("05");
		mav.addObject("cd1List", comaService.coma104Q(codeVo));
		
		//등급
		codeVo.setCode_p_no("06");
		mav.addObject("cd2List", comaService.coma104Q(codeVo));

		return mav;
	}

	@RequestMapping
	public ModelAndView insertUsr(@RequestParam Map vo, HttpServletRequest req,@RequestParam String[] hps,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();

		if(hps.length==0){
			jsonMap.put("returnCd", "2");

			mav.addObject("map", jsonMap);
			
			return mav;
		}

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn")) && "Y".equals(vo.get("roll2"))){
			List list = new ArrayList();
			for(int i =0;i < hps.length;i ++){
				list.add(hps[i]);
			}
			vo.put("list", list);
			
			List samList = service.updateUsrList(vo);
			if (samList.size() > 0) {
				jsonMap.put("returnCd", "1");
				jsonMap.put("list", samList);
			} else {
				vo.put("mem_seq", Integer.toString(service.insertUsr(vo)));
				for(int i =0;i < hps.length;i ++){
					vo.put("hp", hps[i]);
					service.insertUsrHp(vo);	
				}
				
				jsonMap.put("returnCd", "0");
			}
			mav.addObject("map", jsonMap);
		}
		
		
		return mav;
	}

	@RequestMapping
	public ModelAndView udtUsr(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView();

		COMAVo codeVo = new COMAVo();
		// 가입사이트
		codeVo.setCode_p_no("05");
		mav.addObject("cd1List", comaService.coma104Q(codeVo));
		
		//등급
		codeVo.setCode_p_no("06");
		mav.addObject("cd2List", comaService.coma104Q(codeVo));
		
		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn"))){
			mav.addObject("map", service.getUsr(vo));
		}
		
		return mav;
	}

	@RequestMapping
	public ModelAndView deleteInfo(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("/common/close");
		mav.addObject("redirect", "./main.do");

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn"))){
			service.deleteInfo(vo);
			mav.addObject("msg", "삭제되었습니다");
		}
		
		return mav;
	}
	

	@RequestMapping
	public ModelAndView updateUsr(@RequestParam Map vo, HttpServletRequest req,@RequestParam String[] hps,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn")) && "Y".equals(vo.get("roll2"))){
			List list = new ArrayList();
			for(int i =0;i < hps.length;i ++){
				list.add(hps[i]);
			}
			vo.put("list", list);
			List samList = service.updateUsrList(vo);
			if (samList.size() > 0) {
				jsonMap.put("returnCd", "1");
				jsonMap.put("list", samList);
			} else {

				//기존 데이터 지우고
				service.deleteUsrHp(vo);
				//수정
				service.updateUsr(vo);
				//휴대폰 번호 추가
				for(int i =0;i < hps.length;i ++){
					vo.put("hp", hps[i]);
					service.insertUsrHp(vo);	
				}
				jsonMap.put("returnCd", "0");
			}
			mav.addObject("map", jsonMap);
		}else{
			jsonMap.put("returnCd", "2");
			mav.addObject("map", jsonMap);
		}
		
		return mav;
	}
	
	

	@RequestMapping
	public ModelAndView usrLog(@RequestParam Map vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("common/toJson");
		Map jsonMap = new HashMap();

		vo = dataDesc(vo, req,service);
		
		if("Y".equals(vo.get("chkYn"))){
			service.insertUsrLog(vo);
		}
		
		return mav;
	}

}
